/*
package com.example.springbootmybatis.commons.socket;

import com.alibaba.fastjson.JSON;
import com.example.springbootmybatis.commons.tools.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;


*/
/**
 * @author zhouyu
 * @since 2020/01/07
 * WebSocketServer
 *//*

@ServerEndpoint(value = "/websocket/{userId}")
@Component
public class WebSocketServer {
    private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class);
    //WebSocket会话
    private Session session;

    //WebSocket会话列表 K:UserId V:WebSocket会话
    private static ConcurrentHashMap<String, Session> sessionPool = new ConcurrentHashMap<>();

    //WebSocket对应的用户列表 K:sessionId V:userId
    private static ConcurrentHashMap<String, String> sessionIds = new ConcurrentHashMap<>();

    private static ITUsersService usersService;

    @Autowired
    public void setUsersService(ITUsersService usersService) {
        WebSocketServer.usersService = usersService;
    }

    @OnOpen
    public void open(Session session, @PathParam(value = "userId") String userId) throws IOException {
        if (StringUtil.isEmpty(userId)){
            logger.warn("userId为空,建立socket连接失败");
            session.close();
            return;
        }
        TUsers user = usersService.getById(userId);
        if (user == null){
            logger.warn("用户不存在,建立socket连接失败");
            session.close();
            return;
        }
        setSession(session);
        sessionPool.put(userId, session);
        sessionIds.put(session.getId(),userId);
        logger.info("userId:"+userId+"已连接.当前会话人数:"+sessionPool.size());
    }

    @OnMessage
    public void onMessage(String message) {
        logger.info("收到消息:"+message);
        if (StringUtil.isEmpty(message)){
            return;
        }
        //解析消息类型
        WebSocketMessage socketMessage =  JSON.parseObject(message,WebSocketMessage.class);
        //获取接收方的用户id
        String receiverUserId =  socketMessage.getToName();
        Boolean userExist =  sessionPool.containsKey(receiverUserId);
        if (userExist){
            //发送消息
            String responseMessage = socketMessage.getFlag() + "||"+ socketMessage.getMessage();
            sendMessage(receiverUserId,responseMessage);
            return;
        }
        logger.warn("接收者userId:"+receiverUserId+"不存在于会话列表中");
    }

    @OnClose
    public void onClose() {
        String userId = sessionIds.get(session.getId());
        sessionPool.remove(userId);
        sessionIds.remove(session.getId());
        logger.error("userId:"+userId+"断开连接");
    }

    @OnError
    public void onError(Session session, Throwable error) {
        error.printStackTrace();
        logger.error("sessionId:"+session.getId()+"连接错误",error);
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public synchronized static void sendMessage(String userId,String message) {
        Session s = sessionPool.get(userId);
        if (s != null) {
            try {
                s.getBasicRemote().sendText(message);
                logger.info("向userId为:"+userId+"发送消息:"+message+"成功");
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("向userId为:"+userId+"发送消息:"+message+"抛出IO异常",e);
            }
        }
    }
}

*/
